package com.gujia.concurrency.example.singleton;

import com.gujia.concurrency.annoations.NoThreadSafe;
import com.gujia.concurrency.annoations.NotRecommend;
import com.gujia.concurrency.annoations.ThreadSafe;

/**
 * 懒汉模式
 * 单例实例再第一次使用时进行创建
 * 双重同步锁
 */
@ThreadSafe
@NotRecommend
public class SingletonExample5 {


    private SingletonExample5(){

    }

    //1,memory = allocate() 分配对象的内存空间
    //2,ctorInstance() 初始话对象
    //3. instance = memory 设置instance指向刚分配的内存

    //单例模式 volatile + 双重检测机制  -> 进制指令重排
    private volatile static SingletonExample5 instance = null;

    public static SingletonExample5 getInnstance(){
        if(instance == null){
            synchronized(SingletonExample5.class){
                if(instance == null){
                    instance = new SingletonExample5();
                }
            }

        }

        return instance;
    }


}
